/*
 * @Description:
 * @Author: yucongshuang
 * @Date: 2022-10-13 11:03:09
 * @LastEditTime: 2022-11-10 15:35:49
 * @LastEditors:
 */
import axios from 'axios';
import qs from 'qs';
// import { Message } from 'ant-design-vue';
import { Message } from 'element-ui';
const ajax=axios.create({
});
ajax.interceptors.request.use(config=> {
    return config;
}, err=> {
    console.log(err);
});
ajax.interceptors.response.use(data=> {
    if (data.status && data.status == 200 && data.data.code != 0 && data.data.Message) {
        Message.error( data.data.Message);
    }
    if(!data.data.code && !data.data.Message && !data.data.data){
        return data.data;
    }
    return  data.data;
}, err=> {
    if(err.response){
        if (err.response.status == 504||err.response.status == 404) {
          // Message.error('服务器被吃了!');
        }else {
          Message.error(err.response.data.Message || err.response.data.msg || "未知错误!");
        }
    }
    else{
      // Message.error('未知错误!');
    }
    return false;
});

/**
 *
 * @param {*} param0
 * url请求地址
 * method请求方式
 * data参数
 * isPureText是否流数据
 * isCatchError是否页面内部处理错误信息，默认为false,为false就统一在response处理
 * @returns
 */
const $http=({
    url,
    method='get',
    data={},
    responseType='application/json',
    isPuretext=false,
    contentType='application/json;charset=utf-8',
    isCatchError=false,
    headers = {} // 增加 headers 参数
  }) => {
    const path=`${_ctxPath}${url}`;
    if(isPuretext){
        return axios.get(path, {responseType: 'text'});
    }
    if(method=='put' || method=='post'){
        return ajax({
            method: method,
            url: path,
            data: data,
            responseType: responseType,
            headers: {
                'Content-Type': contentType,
                ...headers // 合并传入的 headers
            }
        });
    }
    if(method=='get' || method=='delete'){
        return ajax({
            method: method,
            url: path,
            params: data,
            responseType: responseType,
            headers: {
                'Content-Type': contentType,
                ...headers // 合并传入的 headers
            }
        });
    }

};
export default  $http;
